//
//  BTKMarketView.m
//  BTCoin
//
//  Created by Robin on 19/03/2018.
//  Copyright © 2018 Robin. All rights reserved.
//

#import "BTKMarketView.h"
#import "MarketListTableViewCell.h"
#import "MarketHomeListModel.h"
#define kTableviewHeight kScreenHeight - 65 - kNavToolBarHeight - kTabBarHeight

@interface BTKMarketView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UIImageView * ovalView;
@property (nonatomic,strong) UITableView * usdtTabeView;
@property (nonatomic,strong) UITableView * btcTabeView;
@property (nonatomic,strong) UITableView * ethTabeView;
@property (nonatomic,assign) MarketTradeType tradeType;
@property (nonatomic, strong) UIView    *tableTitleView;
@end

@implementation BTKMarketView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        self.tradeType = USDT_Type;
        
        UIView * headerView = [[UIView alloc] init];
        headerView.backgroundColor = UIColorFromRGB(0x00192A);
        [self addSubview:headerView];
        [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.top.trailing.equalTo(self);
            make.height.mas_equalTo(45.0);
        }];
        
        self.ovalView = [[UIImageView alloc] init];
        self.ovalView.backgroundColor = UIColorFromRGB(0x002744);
        [headerView addSubview:self.ovalView];
        [self.ovalView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(headerView);
            make.height.mas_equalTo(45.0);
            make.centerY.equalTo(headerView);
            make.width.mas_equalTo(ScreenWidth/3);
        }];
        
        UILabel * USDTLabel = [[UILabel alloc] init];
        [USDTLabel setFont:[UIFont systemFontOfSize:17]];
        [USDTLabel setTextColor:COLOR_Bg_White];
        USDTLabel.text = NSLocalizedString(@"USDT交易", nil);
        [USDTLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:USDTLabel];
        [USDTLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(headerView);
            make.centerY.equalTo(headerView);
        }];
        
        UIButton * USDTBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        USDTBtn.tag = USDT_Type;
        USDTBtn.backgroundColor = [UIColor clearColor];
        [USDTBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:USDTBtn];
        [USDTBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(USDTLabel);
        }];
        
        UILabel * BTCLabel = [[UILabel alloc] init];
        [BTCLabel setFont:[UIFont systemFontOfSize:17]];
        [BTCLabel setTextColor:COLOR_Bg_White];
        BTCLabel.text = NSLocalizedString(@"BTC交易", nil);
        [BTCLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:BTCLabel];
        [BTCLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(USDTLabel);
            make.leading.equalTo(USDTLabel.mas_trailing);
            make.width.equalTo(USDTLabel.mas_width);
        }];
        
        UIButton * BTCBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        BTCBtn.tag = BTC_Type;
        BTCBtn.backgroundColor = [UIColor clearColor];
        [BTCBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:BTCBtn];
        [BTCBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(BTCLabel);
        }];
        
        UILabel * ETHLabel = [[UILabel alloc] init];
        [ETHLabel setFont:[UIFont systemFontOfSize:17]];
        [ETHLabel setTextColor:COLOR_Bg_White];
        ETHLabel.text = NSLocalizedString(@"ETH交易", nil);
        [ETHLabel setTextAlignment:NSTextAlignmentCenter];
        [headerView addSubview:ETHLabel];
        [ETHLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(USDTLabel);
            make.leading.equalTo(BTCLabel.mas_trailing);
            make.trailing.equalTo(headerView);
            make.width.equalTo(USDTLabel.mas_width);
        }];
        
        UIButton * ETHBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        ETHBtn.tag = ETH_Type;
        ETHBtn.backgroundColor = [UIColor clearColor];
        [ETHBtn addTarget:self action:@selector(titleAction:) forControlEvents:UIControlEventTouchUpInside];
        [headerView addSubview:ETHBtn];
        [ETHBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.leading.bottom.trailing.equalTo(ETHLabel);
        }];
        
        UIView * tableTitleView = [[UIView alloc] init];
        tableTitleView.backgroundColor = UIColorFromRGB(0xEEEEEE);
        [self addSubview:tableTitleView];
        [tableTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.trailing.equalTo(self);
            make.top.equalTo(headerView.mas_bottom);
            make.height.mas_equalTo(20.0);
        }];
        self.tableTitleView = tableTitleView;
        
        [self addSubview:self.usdtTabeView];
        [self addSubview:self.btcTabeView];
        [self addSubview:self.ethTabeView];
    
    }
    
    return self;
}

#pragma mark - getter
- (UITableView *)usdtTabeView {
    if (!_usdtTabeView) {
        _usdtTabeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, kScreenWidth, kTableviewHeight) style:UITableViewStylePlain];
        _usdtTabeView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _usdtTabeView.delegate = self;
        _usdtTabeView.dataSource = self;
        [_usdtTabeView registerNib:[UINib nibWithNibName:@"MarketListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MarketListTableViewCell"];
    }
    return _usdtTabeView;
}

- (UITableView *)btcTabeView {
    if (!_btcTabeView) {
        _btcTabeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, kScreenWidth, kTableviewHeight) style:UITableViewStylePlain];
        _btcTabeView.hidden = YES;
        _btcTabeView.delegate = self;
        _btcTabeView.dataSource = self;
        [_btcTabeView registerNib:[UINib nibWithNibName:@"MarketListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MarketListTableViewCell"];
    }
    return _btcTabeView;
}

- (UITableView *)ethTabeView {
    if (!_ethTabeView) {
        _ethTabeView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, kScreenWidth, kTableviewHeight) style:UITableViewStylePlain];
        _ethTabeView.hidden = YES;
        _ethTabeView.dataSource = self;
        _ethTabeView.delegate = self;
        [_ethTabeView registerNib:[UINib nibWithNibName:@"MarketListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"MarketListTableViewCell"];
    }
    return _ethTabeView;
}
#pragma mark - event actions
- (void)titleAction:(UIButton *)sender {
    
    [self.ovalView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(sender.tag*(self.width/3));
    }];

    self.usdtTabeView.hidden = YES;
    self.btcTabeView.hidden = YES;
    self.ethTabeView.hidden = YES;
    
    if (self.tradeType == USDT_Type) {
        self.usdtTabeView.hidden = NO;
    } else if (self.tradeType == BTC_Type) {
        self.btcTabeView.hidden = NO;
    } else if (self.tradeType == ETH_Type) {
        self.ethTabeView.hidden = NO;
    }
    
    if (self.block) {
        self.block(sender.tag);
    }
}

- (void)reloadTableviewWithSourceArr:(NSArray *)sourceArr {
    self.sourceArr = sourceArr;
    if (self.tradeType == USDT_Type) {
        [self.usdtTabeView reloadData];;
    } else if (self.tradeType == BTC_Type) {
        [self.btcTabeView reloadData];
    } else if (self.tradeType == ETH_Type) {
        [self.ethTabeView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MarketListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MarketListTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MarketListTableViewCell" owner:nil options:nil] firstObject];
    }
    MarketHomeListContentModel *model = self.sourceArr[indexPath.row];
    [cell setMarketModel:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 56;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MarketHomeListContentModel *model = self.sourceArr[indexPath.row];
    if (self.delegate && [self.delegate respondsToSelector:@selector(JumpToMarketListCellActionWithType:)]) {
        [self.delegate JumpToMarketListCellActionWithType:model.fCoinType];
    }
}
@end
